package com.example.module_home.Adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.module_home.R;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by 陈煜 on 2018/5/17.
 */

public class SearchContentAdapter extends ArrayAdapter<String> {
    private Context mContext;
    private List<List<Integer>> mIndexes;

    public SearchContentAdapter(@NonNull Context context,@NonNull List<String> titles,List<List<Integer>> indexes) {
        super(context, R.layout.home_item_search_content,titles);
        this.mContext = context;
        this.mIndexes = indexes;
    }


    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        String title = getItem(position);
        View view;
        ViewHolder holder;
        if(convertView==null){
            view = LayoutInflater.from(mContext).inflate(R.layout.home_item_search_content,parent,false);
            holder = new ViewHolder();
            holder.node_value = view.findViewById(R.id.node_value);
            view.setTag(holder);
        }else {
            view = convertView;
            holder = (ViewHolder) view.getTag();
        }
        SpannableStringBuilder spanString = new SpannableStringBuilder(title);
        for(Integer index:mIndexes.get(position)){
            spanString.setSpan(new ForegroundColorSpan(Color.RED),index,index+1,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        holder.node_value.setText(spanString);
        return view;
    }

    class ViewHolder{
        TextView node_value;
    }
}
